package com.shire.shire_interface.controller.validation;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.validation.WxValidationRule;
import com.shire.shire_interface.model.validation.WxValidationRuleExample;
import com.shire.shire_interface.service.validation.WxValidationRuleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

//@Api(tags = "加好友 好友验证")
@RestController
@RequestMapping("/validation")
public class WxValidationRuleController extends BaseController {
    @Resource
    private WxValidationRuleService wxValidationRuleService;

//    @ApiOperation(value = "获取设置推荐值 csId, autoPass:0：手动 1 自动, sleepStart, sleepEnd")
    @ResponseBody
    @PostMapping("/insertSetting")
    public Response insertSetting(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            String wxAccount = jsonObject.getString("wxAccount");
            Integer sleepStart = jsonObject.getInteger("sleepStart");
            Integer sleepEnd = jsonObject.getInteger("sleepEnd");
            Integer autoPass = jsonObject.getInteger("autoPass");

            WxValidationRuleExample example = new WxValidationRuleExample();
            example.createCriteria().andWxaccountEqualTo(wxAccount);
            List<WxValidationRule> wxValidationRules = wxValidationRuleService.selectByExample(example);
            WxValidationRule rule;
            if (wxValidationRules.size() > 0) {
                rule = wxValidationRules.get(0);
            }
            else
                rule = new WxValidationRule();

            rule.setWxaccount(wxAccount);
            rule.setAutopass(autoPass);
            rule.setSleepstart(sleepStart);
            rule.setSleepend(sleepEnd);
            if (rule.getId() == null)
                wxValidationRuleService.insert(rule);
            else {
                rule.setUpdateTime(new Date());
                wxValidationRuleService.update(rule);
            }

            response.setData(rule);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }


//    @ApiOperation(value = "获取设置推荐值 csId, wxAccount")
    @ResponseBody
    @PostMapping("/get")
    public Response getValidataionRule(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            String wxAccount = jsonObject.getString("wxAccount");
            WxValidationRuleExample example = new WxValidationRuleExample();
            example.createCriteria().andWxaccountEqualTo(wxAccount);
            List<WxValidationRule> wxValidationRules = wxValidationRuleService.selectByExample(example);
            if (wxValidationRules.size() > 0)
                response.setData(wxValidationRules.get(0));
            else {
                WxValidationRule rule = new WxValidationRule();
                rule.setCsid(csId);
                rule.setAutopass(0);
                rule.setSleepstart(120);
                rule.setSleepend(240);
                response.setData(rule);
            }
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }
}
